package com.cet.designpatterns.factory.simplefactory.pizzastore.order;

import com.cet.designpatterns.factory.simplefactory.pizzastore.pizza.CheesePizza;
import com.cet.designpatterns.factory.simplefactory.pizzastore.pizza.GreekPizza;
import com.cet.designpatterns.factory.simplefactory.pizzastore.pizza.PepperPizza;
import com.cet.designpatterns.factory.simplefactory.pizzastore.pizza.Pizza;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class SimplePizzaFactory {

    public static Pizza create(Class<? extends Pizza> cls,String pizzaName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        System.out.println("使用简单工厂模式~~");
        Constructor<? extends Pizza> constructor = cls.getConstructor();
        Pizza pizza = constructor.newInstance(null);
        Method setName = pizza.getClass().getMethod("setName", String.class);
        setName.invoke(pizza, pizzaName);
        return pizza;
    }

    public Pizza createPizza(String orderType) {
        System.out.println("使用简单工厂模式~~");
        Pizza pizza = null;
        if ("greek".equals(orderType)) {
            pizza = new GreekPizza();
            pizza.setName("希腊披萨");
        } else if ("cheese".equals(orderType)) {
            pizza = new CheesePizza();
            pizza.setName("奶酪披萨");
        } else if ("pepper".equals(orderType)) {
            pizza = new PepperPizza();
            pizza.setName("胡椒披萨");
        }
        return pizza;
    }

    //简单工厂模式也叫静态工厂模式

    public static Pizza createPizza2(String orderType) {
        System.out.println("使用简单工厂模式~~");
        Pizza pizza = null;
        if ("greek".equals(orderType)) {
            pizza = new GreekPizza();
            pizza.setName("希腊披萨");
        } else if ("cheese".equals(orderType)) {
            pizza = new CheesePizza();
            pizza.setName("奶酪披萨");
        } else if ("pepper".equals(orderType)) {
            pizza = new PepperPizza();
            pizza.setName("胡椒披萨");
        }
        return pizza;
    }
}
